# 文件处理
# 读
# 获取文件对象   r对后面的字符串  反转义
import os

import openpyxl
from common.conf import data_path
from openpyxl.workbook import Workbook
from openpyxl.worksheet.worksheet import Worksheet

from common.dolog import Dolog

dolog=Dolog()
myLog=dolog.getLogger(__name__)
class Dofile():
    def readFile(self,fname):
        # myFile = rf'C:\python\autoTest\datas\{fname}'
        myFile = os.path.join(data_path, fname)
        with open(myFile,mode='r',encoding='utf-8') as f:
            readStr = f.read()
            print(readStr)
        return readStr

    def wirteFile(self,fname,str):
        # myFile = rf'C:\python\autoTest\datas\{fname}'
        myFile = os.path.join(data_path,'fname')
        with open(myFile,mode='w',encoding='utf-8') as f:
            wirteStr = f.write(str)
            print(wirteStr)
        return wirteStr

    def readExcel(self,excel,Sname,minR,maxR,minC,maxC,values_only=True):
        # myFile =rf'C:\python\autoTest\datas\{excel}'
        myFile = os.path.join(data_path,excel)
        wk:Workbook=openpyxl.load_workbook(myFile,read_only=True)
        st:Worksheet = wk[Sname]

        #读取全部
        # print(st.values)
        # vs = [i for i in st.values]

        #读取部分
        vs = [i for i in st.iter_rows(minR,maxR,minC,maxC,values_only=True)]
        print(vs)
        return vs

    def wirteExcel(self,excel,Sname):
        wk:Workbook=openpyxl.Workbook()
        st:Worksheet=wk.create_sheet(Sname)
        st.cell(1,1,'aaa')
        st.cell(1,2,'bbb')
        st.cell(2,1,'aaa1')
        st.cell(2,2,'bbb1')

        # myFile = rf'C:\python\autoTest\datas\{excel}'
        myFile = os.path.join(data_path, excel)
        wk.save(myFile)

if __name__ == '__main__':
    # l = Dofile()
    # l.readExcel('aaa.xlsx')
    # l.readExcel('aaa.xlsx',2,4,2,3)
    # l.wirteExcel('bbb.xlsx')
    r = Dofile()
    # r.readFile('test1.txt')
    r.wirteFile('test2.txt','aaa\nbbb')
    # myFile = r'C:\python\autoTest\dates\test_ZiDongHua.xlsx'
    # f = open(myFile,mode='r',encoding='utf-8')
    # print(f.read())

    # f.close()

    # 写文件（可存在，或者不存在）
    # 如果存在：覆盖重新写
    # 如果不存在：创建

    # myFile = r'C:\python\autoTest\dates\test_ZiDongHua.xlsx'
    # f = open(myFile,mode='w',encoding='utf-8')
    # f.write('aaa\nbbbb\nccccc\nddddd')

    # f.close()


    #文件不存在，新建
    # myFile = r'C:\python\autoTest\dates\test_ZiDongHua.xlsx'
    # f = open(myFile,mode='w',encoding='utf-8')
    # f.write('111111\n22222222222\n33333333')

    # f.close()


    #追加
    # myFile = r'C:\python\autoTest\dates\test_ZiDongHua.xlsx'
    # f = open(myFile,mode='a',encoding='utf-8')
    # f.write('\n111111\n22222222222\n33333333')

    # f.close()